/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controle;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Aluno
 */
public class Filme extends javax.swing.JFrame {
    private Object film;

    /**
     * Creates new form NewJFrame
     */
    public Filme() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jfNome = new javax.swing.JTextField();
        jlNome = new javax.swing.JLabel();
        cbCategoria = new javax.swing.JComboBox();
        cbGenero = new javax.swing.JComboBox();
        jlCategoria = new javax.swing.JLabel();
        jlGenero = new javax.swing.JLabel();
        jbCadastrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jfDuracao = new javax.swing.JTextField();
        jbAlterar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jfId = new javax.swing.JTextField();
        jbExcluir = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbConsulta2 = new javax.swing.JTable();
        jbConsultar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jfNome.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jfNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jfNomeActionPerformed(evt);
            }
        });
        jfNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jfNomeKeyTyped(evt);
            }
        });

        jlNome.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jlNome.setText("Nome:");

        cbCategoria.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        cbCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Classico", "Lançamento", "Em cartaz", "Brasileiro", "Reprizado" }));
        cbCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCategoriaActionPerformed(evt);
            }
        });

        cbGenero.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        cbGenero.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Animação", "Romance", "Terror", "Comédia", "Ação", "Suspense" }));
        cbGenero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbGeneroActionPerformed(evt);
            }
        });

        jlCategoria.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jlCategoria.setText("Categoria:");

        jlGenero.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jlGenero.setText("Gênero:");

        jbCadastrar.setText("Cadastrar");
        jbCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCadastrarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jLabel1.setText("Duração:");

        jbAlterar.setText("Alterar");
        jbAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAlterarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jLabel2.setText("Id:");

        jfId.setEditable(false);
        jfId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jfIdActionPerformed(evt);
            }
        });

        jbExcluir.setText("Excluir");
        jbExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExcluirActionPerformed(evt);
            }
        });

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Contatos"));

        jtbConsulta2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Nome do Filme", "Duração do Filme", "Genero", "Categoria"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtbConsulta2.getTableHeader().setReorderingAllowed(false);
        jtbConsulta2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jtbConsulta2MouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(jtbConsulta2);

        jbConsultar.setText("Consulta");
        jbConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConsultarActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
        jLabel3.setText("Cadastrar Filme");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                        .addGap(1, 1, 1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jbCadastrar)
                                .addGap(58, 58, 58)
                                .addComponent(jbAlterar)
                                .addGap(33, 33, 33))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel1)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jlNome, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(28, 28, 28)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jfDuracao, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jfId, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(10, 10, 10)
                                                .addComponent(cbGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jlGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jlCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGap(16, 16, 16)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbConsultar)
                        .addGap(72, 72, 72)
                        .addComponent(jbExcluir)
                        .addGap(99, 99, 99))))
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jLabel3)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jfId, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlNome, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jfDuracao, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(102, 102, 102)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCadastrar)
                    .addComponent(jbAlterar)
                    .addComponent(jbConsultar)
                    .addComponent(jbExcluir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jfNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jfNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jfNomeActionPerformed

    private void jfNomeKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jfNomeKeyTyped
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jfNomeKeyTyped

    private void cbCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCategoriaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbCategoriaActionPerformed

    private void cbGeneroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbGeneroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbGeneroActionPerformed

    private void jbCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCadastrarActionPerformed
        // TODO add your handling code here:

        if(jfNome.getText().isEmpty()){
        
            JOptionPane.showMessageDialog(rootPane, "Nome inválido.");
        }
        else{
            filme.setTit_filme(jfNome.getText());
            filme.setDur_filme(jfDuracao.getText());
            String gen = cbGenero.getSelectedItem().toString();
            String cat = cbCategoria.getSelectedItem().toString();
            filme.setGenero(cat);
            filme.setCategoria(cat);
            
            
            
    }                                              
            
        
        

        filme_dao.inserir(film);
        ArrayList<Filme> filme = filme_dao.listar();
        excluirLinhasNaTabela(); //excluir linhas na tabela atual
        incluirLinhasNaTabela(filme); //incluir linhas na tabela para que elas possam receber os dados da consulta atual
        preencherTabela(filme); //colocar os valores nas linhas
    }//GEN-LAST:event_jbCadastrarActionPerformed

    private void jbAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAlterarActionPerformed

    }//GEN-LAST:event_jbAlterarActionPerformed

    private void jfIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jfIdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jfIdActionPerformed

    private void jbExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExcluirActionPerformed
        // TODO add your handling code here:
       filmeDAO.excluir(filme.getCod_filme());
            ArrayList<Filme> filmes;
            filmes = filmeDAO.listar();
            //preencher a tabela, inclui:
            excluirLinhasNaTabelaF(); //excluir linhas na tabela atual
            incluirLinhasNaTabelaFilme(filmes); //incluir linhas na tabela para que elas possam receber os dados da consulta atual
            preencherTabelaFilme(filmes); //colocar os valores nas linhas
            
            limparCampos(); 
    }//GEN-LAST:event_jbExcluirActionPerformed

    private void jtbConsulta2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbConsulta2MouseReleased

        int linhaSelecionada = jtbConsulta2.getSelectedRow(); //retorna um inteiro
        jfId.setText(jtbConsulta2.getValueAt(linhaSelecionada,0).toString());
        jfNome.setText(jtbConsulta2.getValueAt(linhaSelecionada, 1).toString());
        jfDuracao.setText(jtbConsulta2.getValueAt(linhaSelecionada, 2).toString());
        film.setTitulo_filme(jtbConsulta2.getValueAt(linhaSelecionada, 1).toString());
        film.setDuracao_filme(Long.parseLong(jtbConsulta2.getValueAt(linhaSelecionada, 2).toString()));
        cbGenero.setSelectedItem(jtbConsulta2.getValueAt(linhaSelecionada, 3).toString());
        cbCategoria.setSelectedItem(jtbConsulta2.getValueAt(linhaSelecionada, 4).toString());

        film.setCod_filme(Integer.parseInt(jtbConsulta2.getValueAt(linhaSelecionada, 0).toString()));
        System.out.println(film.getTitulo_filme());
        System.out.println(film.getCod_filme());        // TODO add your handling code here:
    }//GEN-LAST:event_jtbConsulta2MouseReleased

    private void jbConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConsultarActionPerformed

        if( jfNome.getText().isEmpty() ){ //se o campo está vazio
            JOptionPane.showMessageDialog(rootPane, "Nome inválido!");
        }
        else{
            ArrayList<Filme> filmes = filme_dao.consultar(jfNome.getText());
            if(filmes.isEmpty()){ //se há registros no banco de dados para a chave consultada
                JOptionPane.showMessageDialog(rootPane, "O contato não está cadastrado.");
            }
            else{
                //preencher a tabela, inclui:
                excluirLinhasNaTabela(); //excluir linhas na tabela atual
                incluirLinhasNaTabela(filmes); //incluir linhas na tabela para que elas possam receber os dados da consulta atual
                preencherTabela(filmes); //colocar os valores nas linhas
                preencherTabela(filmes);
            }
        }
    }//GEN-LAST:event_jbConsultarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Filme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Filme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Filme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Filme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Filme().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbCategoria;
    private javax.swing.JComboBox cbGenero;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbAlterar;
    private javax.swing.JButton jbCadastrar;
    private javax.swing.JButton jbConsultar;
    private javax.swing.JButton jbExcluir;
    private javax.swing.JTextField jfDuracao;
    private javax.swing.JTextField jfId;
    private javax.swing.JTextField jfNome;
    private javax.swing.JLabel jlCategoria;
    private javax.swing.JLabel jlGenero;
    private javax.swing.JLabel jlNome;
    private javax.swing.JTable jtbConsulta2;
    // End of variables declaration//GEN-END:variables

  Filme filme = new Filme();
 
  Ator ator = new  Ator();
  AtorDAO atorDAO = new AtorDAO();

    private void setTit_filme(String text) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void setDur_filme(String text) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void setGenero(String cat) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void setCategoria(String cat) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
